package com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper;

import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.COrderDetail;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * <p>
 * 余额信息表 Mapper 接口
 * </p>
 *
 * @author xiaoxu
 * @since 2022-12-27
 */
//todo 这里的充值我值用了sob1还没实现多个闸口统一余额
@Component
public interface COrderDetailMapper extends BaseMapper<COrderDetail> {

    @Update("UPDATE c_order_detail \n" +
            "SET ORDETAIL_ITEM_NUM = COALESCE(ORDETAIL_ITEM_NUM, 0) + #{financeEnterNum},\n" +
            "    ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) + #{financeEnterNum},\n" +
            "    ORDETAIL_LASTIN_NUM = #{financeEnterNum},\n" +
            "    ORDETAIL_SOB1_NUM = COALESCE(ORDETAIL_SOB1_NUM, 0) + #{financeEnterNum},\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) + #{financeEnterNum},\n" +
            "    ORDETAIL_SOB1_LASTIN_NUM = #{financeEnterNum},\n" +
            "    ORDETAIL_MODIFY_TIME = NOW(),\n" +
            "    DATA_FROM = #{plantId},\n" +
            "    ORDETAIL_DELETED = 0\n" +
            "WHERE ID = #{ordetailId};")
    void insertOrderDetail(@Param("financeEnterNum") String financeEnterNum, @Param("plantId") String plantId, @Param("ordetailId") String ordetailId);
//老调价方式
    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) + (#{oldMoney} - #{newMoney}),\n" +
            "    ORDETAIL_LASTIN_NUM = COALESCE(ORDETAIL_LASTIN_NUM, 0) + (#{newMoney} - #{oldMoney}),\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) + (#{newMoney} - #{oldMoney}),\n" +
            "    ORDETAIL_SOB1_LASTIN_NUM = COALESCE(ORDETAIL_SOB1_LASTIN_NUM, 0) + (#{newMoney} - #{oldMoney}),\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE ID = #{cOrderDetailId}")
    void updateOrderDetail(@Param("oldMoney") BigDecimal oldMoney, @Param("newMoney") BigDecimal newMoney, @Param("cOrderDetailId") String cOrderDetailId);


//老调价方式
    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) + (#{oldMoney} - #{newMoney}),\n" +
            "    ORDETAIL_LASTIN_NUM = COALESCE(ORDETAIL_LASTIN_NUM, 0) + (#{newMoney} - #{oldMoney}),\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = COALESCE(ORDETAIL_SOB2_REMAIN_NUM, 0) + (#{newMoney} - #{oldMoney}),\n" +
            "    ORDETAIL_SOB1_LASTIN_NUM = COALESCE(ORDETAIL_SOB2_LASTIN_NUM, 0) + (#{newMoney} - #{oldMoney}),\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE ID = #{cOrderDetailId}")
    void updateOrderDetailNegative(@Param("oldMoney") BigDecimal oldMoney, @Param("newMoney") BigDecimal newMoney, @Param("cOrderDetailId") String cOrderDetailId);

//适用负价格调价方式
    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) + #{oldMoney},\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = CASE WHEN #{oldMoney} > 0 THEN COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) + #{oldMoney} ELSE ORDETAIL_SOB1_REMAIN_NUM END,\n" +
            "    ORDETAIL_SOB2_REMAIN_NUM = CASE WHEN #{oldMoney} < 0 THEN COALESCE(ORDETAIL_SOB2_REMAIN_NUM, 0) + #{oldMoney} ELSE ORDETAIL_SOB2_REMAIN_NUM END,\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE ID = #{cOrderDetailId}")
    void refundOldDeduction(@Param("oldMoney") BigDecimal oldMoney, @Param("cOrderDetailId") String cOrderDetailId);

    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) - #{newMoney},\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = CASE WHEN #{newMoney} > 0 THEN COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) - #{newMoney} ELSE ORDETAIL_SOB1_REMAIN_NUM END,\n" +
            "    ORDETAIL_SOB2_REMAIN_NUM = CASE WHEN #{newMoney} < 0 THEN COALESCE(ORDETAIL_SOB2_REMAIN_NUM, 0) - #{newMoney} ELSE ORDETAIL_SOB2_REMAIN_NUM END,\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE ID = #{cOrderDetailId}")
    void applyNewDeduction(@Param("newMoney") BigDecimal newMoney, @Param("cOrderDetailId") String cOrderDetailId);
    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) + #{weishtMoney},\n" +
            "    ORDETAIL_LASTIN_NUM =  #{weishtMoney},\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) + #{weishtMoney},\n" +
            "    ORDETAIL_SOB1_LASTIN_NUM = #{weishtMoney},\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE ID = #{cOrderDetailId}")
    void updateOrderDetailForDelete(@Param("weishtMoney") BigDecimal weishtMoney, @Param("cOrderDetailId") String cOrderDetailId);

    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) - #{weishtMoney},\n" +
            "    ORDETAIL_LASTOUT_NUM = #{weishtMoney},\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) - #{weishtMoney},\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE id = #{cOrderDetailId}")
    void updateOrderDetailForPdtReturnOrExchange(@Param("weishtMoney") BigDecimal weishtMoney, @Param("cOrderDetailId") String cOrderDetailId);

    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) - #{weishtMoney} + #{weishtMoney1},\n" +
            "    ORDETAIL_LASTOUT_NUM = COALESCE(ORDETAIL_LASTOUT_NUM, 0) - #{weishtMoney} + #{weishtMoney1},\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) - #{weishtMoney} + #{weishtMoney1},\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE id = #{cOrderDetailId}")
    void updateOrderDetailForPdtReturnOrExchange2(@Param("weishtMoney") BigDecimal weishtMoney, @Param("weishtMoney1") BigDecimal weishtMoney1, @Param("cOrderDetailId") String cOrderDetailId);

    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) - #{originalWeishtMoney},\n" +
            "    ORDETAIL_LASTOUT_NUM = #{originalWeishtMoney},\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) - #{originalWeishtMoney},\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE id = #{id}")
    void updateOrderDetailForWeightSheetAdvanced(@Param("originalWeishtMoney") BigDecimal originalWeishtMoney, @Param("id") String id);

    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) + #{multiply},\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) + #{multiply},\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE id = #{id}")
    void updateOrderDetailForWeightSheetAdvanced2(@Param("multiply") BigDecimal multiply, @Param("id") String id);

    @Update("UPDATE c_order_detail\n" +
            "SET ORDETAIL_REMAIN_NUM = COALESCE(ORDETAIL_REMAIN_NUM, 0) - #{monery},\n" +
            "    ORDETAIL_LASTOUT_NUM = #{monery},\n" +
            "    ORDETAIL_SOB1_REMAIN_NUM = COALESCE(ORDETAIL_SOB1_REMAIN_NUM, 0) - #{monery},\n" +
            "    ORDETAIL_MODIFY_TIME = NOW()\n" +
            "WHERE ID = #{id}")
    void updateOrderDetailExecuteDeduction(@Param("monery") BigDecimal monery, @Param("id") String id);
}
